package com.zzc.emotisenseclassroom;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.zzc.emotisenseclassroom.dbservice.EmotisenseScore.EmotisenseScoreDbService;
import com.zzc.emotisenseclassroom.dto.emotisenseScore.EmotisenseScoreDTO;
import com.zzc.emotisenseclassroom.utils.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Component
public class StudentScoreGenerator {
    @Autowired
    private EmotisenseScoreDbService emotiSenseScoreRepository;

    public void doInsertScore() {
        // 学生信息列表
        List<StudentInfo> students = JSON.parseArray("[{\"classId\":2022107011,\"email\":\"88778877@gmail.com\",\"phoneNumber\":\"887788877\",\"realName\":\"李铁\",\"schoolId\":666666,\"sex\":2,\"userId\":20228398121},{\"classId\":2022107011,\"email\":\"zengke@qq.com\",\"phoneNumber\":\"1239172131321\",\"realName\":\"曾可\",\"schoolId\":666666,\"sex\":1,\"userId\":202241765281},{\"classId\":2022107011,\"email\":\"123456@qq.com\",\"phoneNumber\":\"1731536989\",\"realName\":\"鲜大东\",\"schoolId\":666666,\"sex\":0,\"userId\":20224180213213},{\"avatar\":\"https://i0.hdslb.com/bfs/archive/3bb5160123aa7be3c7cd89370d2d79b4024d7d60.jpg\",\"classId\":2022107011,\"email\":\"72043968@qq.com\",\"phoneNumber\":\"123123321\",\"realName\":\"大海\",\"schoolId\":666666,\"sex\":1,\"userId\":17315399988},{\"classId\":2022107011,\"email\":\"1231123@wangyi.com\",\"phoneNumber\":\"13128697831\",\"realName\":\"说书人\",\"schoolId\":666666,\"sex\":1,\"userId\":1231511231},{\"classId\":2022107011,\"email\":\"7657312@qq.com\",\"phoneNumber\":\"1231111123\",\"realName\":\"小红\",\"schoolId\":666666,\"sex\":1,\"userId\":202241792131},{\"classId\":2022107011,\"email\":\"123911213@qq.com\",\"phoneNumber\":\"88208820\",\"realName\":\"大壮\",\"schoolId\":666666,\"sex\":0,\"userId\":202280703838},{\"classId\":2022107011,\"email\":\"zengcheng@qq.com\",\"phoneNumber\":\"181782218891\",\"realName\":\"增城\",\"schoolId\":666666,\"sex\":0,\"userId\":202280956832},{\"classId\":2022107011,\"email\":\"981231@qq.com\",\"phoneNumber\":\"12318812311\",\"realName\":\"王洪\",\"schoolId\":666666,\"sex\":1,\"userId\":202241738971},{\"classId\":2022107011,\"email\":\"9123131WX@123.com\",\"phoneNumber\":\"12319123112\",\"realName\":\"王晓\",\"schoolId\":666666,\"sex\":1,\"userId\":2022815832813},{\"avatar\":\"../../../static/img/avatar.jpg\",\"classId\":2022107011,\"realName\":\"zzc\",\"schoolId\":666666,\"sex\":0,\"userId\":202241806666},{\"classId\":2022107011,\"email\":\"9123111LXP@123.com\",\"phoneNumber\":\"12319781231\",\"realName\":\"李学鹏\",\"schoolId\":666666,\"sex\":0,\"userId\":202287928162},{\"classId\":2022107011,\"email\":\"son@qq.com\",\"phoneNumber\":\"19871311231\",\"realName\":\"孙兴民\",\"schoolId\":666666,\"sex\":0,\"userId\":202288365213},{\"classId\":2022107011,\"email\":\"1232111@333.com\",\"phoneNumber\":\"123121309912831\",\"realName\":\"沃特\",\"schoolId\":666666,\"sex\":2,\"userId\":202241491241},{\"avatar\":\"../../../static/img/avatar.jpg\",\"classId\":2022107011,\"email\":\"1397115513@qq.com\",\"phoneNumber\":\"15928736818\",\"realName\":\"\uD83D\uDC3B市委\",\"schoolId\":666666,\"sex\":0,\"userId\":100213213},{\"classId\":2022107011,\"email\":\"23758093@qq.com\",\"phoneNumber\":\"15928688888\",\"realName\":\"小明\",\"schoolId\":666666,\"sex\":1,\"userId\":202241807234},{\"classId\":2022107011,\"email\":\"213123@gmail.com\",\"phoneNumber\":\"12312312412\",\"realName\":\"真智米\",\"schoolId\":666666,\"sex\":0,\"userId\":1231231541},{\"classId\":2022107011,\"email\":\"9812319@qq.cpm\",\"phoneNumber\":\"15982966886\",\"realName\":\"封大尘\",\"schoolId\":666666,\"sex\":1,\"userId\":202280719872},{\"classId\":2022107011,\"email\":\"399123112@123.com\",\"phoneNumber\":\"123141311288\",\"realName\":\"皮克曼\",\"schoolId\":666666,\"sex\":0,\"userId\":2022123813411},{\"classId\":2022107011,\"email\":\"zdt_google@gemail.com\",\"phoneNumber\":\"17833906698\",\"realName\":\"张大同\",\"schoolId\":666666,\"sex\":1,\"userId\":202241398652},{\"classId\":2022107011,\"email\":\"87781@123.com\",\"phoneNumber\":\"88123131\",\"realName\":\"郑智\",\"schoolId\":666666,\"sex\":2,\"userId\":2031231211231},{\"classId\":2022107011,\"email\":\"wangtao@gemail.com\",\"phoneNumber\":\"028-1232311\",\"realName\":\"王涛\",\"schoolId\":666666,\"sex\":1,\"userId\":2022418273131}]", StudentInfo.class);

        // 时间范围
        LocalDate startDate = LocalDate.of(2022, 5, 1);
        LocalDate endDate = LocalDate.of(2028, 10, 31);

        // 生成数据
        List<EmotisenseScoreDTO> scores = generateStudentScores(students, startDate, endDate);

        List<List<EmotisenseScoreDTO>> partition = Lists.partition(scores, ConstantUtil.LIST_SIZE);
        for (List<EmotisenseScoreDTO> tempEmotisenseScoreList : partition) {
            // insertScore
            emotiSenseScoreRepository.insertScore(tempEmotisenseScoreList);
        }
    }

    private static List<EmotisenseScoreDTO> generateStudentScores(List<StudentInfo> students, LocalDate startDate, LocalDate endDate) {
        List<EmotisenseScoreDTO> scores = new ArrayList<>();
        Random random = new Random();

        // 循环学生
        for (StudentInfo student : students) {
            // 每个学生随机选择一种评分策略
            ScoreStrategy strategy = chooseScoreStrategy(student.getUserId());

            // 循环日期
            for (LocalDate currentDate = startDate; !currentDate.isAfter(endDate); currentDate = currentDate.plusDays(1)) {
                // 每天生成 0 到 5 条评分数据
                int numScores = ThreadLocalRandom.current().nextInt(0, 6);

                for (int i = 0; i < numScores; i++) {
                    // 根据策略生成分数
                    int score = strategy.generateScore(random);

                    // 创建评分数据对象
                    EmotisenseScoreDTO scoreDTO = new EmotisenseScoreDTO();
                    scoreDTO.setClassId(student.getClassId());
                    scoreDTO.setClassName("20级计科一班");
                    scoreDTO.setUserId(student.getUserId());
                    scoreDTO.setSchoolId(student.getSchoolId());
                    scoreDTO.setSchoolName("江苏科技大学");
                    scoreDTO.setRealName(student.getRealName());
                    scoreDTO.setScore(score);
                    scoreDTO.setClassDate(Date.from(currentDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));

                    // 添加评分数据
                    scores.add(scoreDTO);
                }
            }
        }

        return scores;
    }

    // 根据学生 ID 选择评分策略
    private static ScoreStrategy chooseScoreStrategy(Long userId) {
        int hash = Math.abs(userId.hashCode() % 8);
        return switch (hash) {
            case 0 -> new ExcellentScoreStrategy();
            case 1 -> new GoodScoreStrategy();
            case 2 -> new AverageScoreStrategy();
            case 3 -> new FluctuatingScoreStrategy();
            case 4 -> new AnotherScoreStrategy();
            case 5 -> new ScoreStrategyV2();
            case 6 -> new ScoreStrategyV3();
            case 7 -> new ScoreStrategyV4();
            default -> throw new IllegalArgumentException("Unexpected hash value: " + hash);
        };
    }

    // 评分策略接口
    interface ScoreStrategy {
        int generateScore(Random random);
    }

    // 优秀评分策略（80-100 分）
    static class ExcellentScoreStrategy implements ScoreStrategy {
        @Override
        public int generateScore(Random random) {
            return random.nextInt(20) + 80; // 80 到 100 之间的随机分数
        }
    }

    // 良好评分策略（60-80 分）
    static class GoodScoreStrategy implements ScoreStrategy {
        @Override
        public int generateScore(Random random) {
            return random.nextInt(21) + 60; // 60 到 80 之间的随机分数
        }
    }

    // 中等评分策略（50-70 分）
    static class AverageScoreStrategy implements ScoreStrategy {
        @Override
        public int generateScore(Random random) {
            return random.nextInt(21) + 50; // 50 到 70 之间的随机分数
        }
    }

    // 波动评分策略（50-100 分）
    static class FluctuatingScoreStrategy implements ScoreStrategy {
        @Override
        public int generateScore(Random random) {
            return random.nextInt(50) + 50; // 50 到 100 之间的随机分数
        }
    }

    static class AnotherScoreStrategy implements ScoreStrategy {
        @Override
        public int generateScore(Random random) {
            return random.nextInt(30) + 60;
        }
    }

    static class ScoreStrategyV2 implements ScoreStrategy{
        @Override
        public int generateScore(Random random) {
            return random.nextInt(30) + 70;
        }
    }

    static class ScoreStrategyV3 implements ScoreStrategy{
        @Override
        public int generateScore(Random random) {
            return random.nextInt(10) + 90;
        }
    }

    static class ScoreStrategyV4 implements ScoreStrategy{
        @Override
        public int generateScore(Random random) {
            return random.nextInt(10) + 50;
        }
    }
}
